<?php
class item_tempgroupchange extends template_item
{
	var $default_name = "Temporary Change Group";
	var $default_description = "Change your group to {group_name} for a limited time";
	
	var $able_to_select_admin = 0;
	
	var $allow_multiple = 1;
	var $can_run_multiple = 1;
	
	var $item_config = array();
	var $description = "";
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item['item_name'] = str_replace('{post}',$this->item_config['add_post'],$this->item['item_name']);
			
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['group_id'] = $this->ipsclass->vars['member_group'];
			$this->item_config['lasting'] = 1;
			$this->item_config['time_unit'] = 86400;
		}
		
		$this->description = str_replace('{group_name}',$this->ipsclass->cache['group_cache'][$this->item_config['group_id']]['g_title'],$this->item['item_description']);
		if(!defined('TEMP_GROUP_UPDATED') && $this->ipsclass->input['request_method'] != 'post' && IPB_THIS_SCRIPT == 'public')
		{
			define('TEMP_GROUP_UPDATED', 1);
			if(USE_MODULES == 1)
			{
				$this->ipsclass->DB->build_query(array('select'=>'id,old_mgroup','from'=>'members','where'=>"end_mgroup > 0 AND end_mgroup < ". time()));
				$this->ipsclass->DB->exec_query();
				if($this->ipsclass->DB->get_num_rows())
				{
					require_once(ROOT_PATH."modules/ipb_member_sync.php");
					$modules = new ipb_member_sync();
					$modules->register_class(&$this);
					while($mem = $this->ipsclass->DB->fetch_row())
					{
						$modules->on_group_change($mem['id'],$mem['old_mgroup']);
					}
					$this->ipsclass->DB->build_and_exec_query(array('update'=>'members','set'=>'mgroup=old_mgroup,old_mgroup=0,end_mgroup=0','where'=>"end_mgroup > 0 AND end_mgroup < ". time()));
				}
			}
			else
			{
				$this->ipsclass->DB->build_and_exec_query(array('update'=>'members','set'=>'mgroup=old_mgroup,old_mgroup=0,end_mgroup=0','where'=>"end_mgroup > 0 AND end_mgroup < ". time()));
			}
		}
	}
	
	function admin_form()
	{
		$groups = array();
		foreach($this->ipsclass->cache['group_cache'] as $group_id => $group)
		{
			if($group_id == $this->ipsclass->vars['guest_group'] || $group_id == $this->ipsclass->vars['auth_group'] || !$group['g_view_board'] || !$group['g_market_view'] || !$group['g_market_use'])
			{
				continue;
			}
			
			if(!$group['g_access_cp'] || $this->able_to_select_admin)
			{
				$groups[] = array($group_id,$group['g_title']);
			}
		}
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"Tags for item name and description",
															"{group_name} = The name of the group",
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Change to which group?</b>",
															$this->ipsclass->adskin->form_dropdown("item[".$this->item['item_id']."][group_id]", $groups, $this->item_config['group_id']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>How long does this group change lasts?</b>",
															$this->ipsclass->adskin->form_simple_input("item[".$this->item['item_id']."][lasting]", $this->item_config['lasting']).$this->ipsclass->adskin->form_dropdown("item[".$this->item['item_id']."][time_unit]", array(array(86400,'Days'),array(604800,'Weeks')), $this->item_config['time_unit']),
													)	);
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['group_id'] = intval($input['group_id']);
		$save['lasting'] = max(0,intval($input['lasting']));
		$save['time_unit'] = $input['time_unit'] == 604800 ? 604800 : 86400;
		
		if(!is_array($this->ipsclass->cache['group_cache'][$save['group_id']]))
		{
			$this->ipsclass->admin->error("Invalid group");
		}
		if($save['group_id'] == $this->ipsclass->vars['guest_group'] || $save['group_id'] == $this->ipsclass->vars['auth_group'])
		{
			$this->ipsclass->admin->error("Group cannot be guest or validating");
		}
		if(!$this->ipsclass->cache['group_cache'][$save['group_id']]['g_view_board'] || !$this->ipsclass->cache['group_cache'][$save['group_id']]['g_market_view'] || !$this->ipsclass->cache['group_cache'][$save['group_id']]['g_market_use'])
		{
			$this->ipsclass->admin->error("Group must be able to view the board and market and use the market");
		}
		if($this->ipsclass->cache['group_cache'][$save['group_id']]['g_access_cp'] && !$this->able_to_select_admin)
		{
			$this->ipsclass->admin->error("You cannot select a group that have access to the ACP");
		}
		
		$item_info['item_config'] = serialize($save);
		
		if($this->ipsclass->DB->field_exists("old_mgroup", "members"))
		{
			$this->ipsclass->DB->sql_change_field("members","old_mgroup","old_mgroup","int NOT NULL","'0'");
			$this->ipsclass->DB->sql_change_field("members","end_mgroup","end_mgroup","int NOT NULL","'0'");
		}
		else
		{
			$this->ipsclass->DB->sql_add_field("members","old_mgroup","int NOT NULL","'0'");
			$this->ipsclass->DB->sql_add_field("members","end_mgroup","int NOT NULL","'0'");
		}
	}
	
	function admin_remove()
	{
		if(count($this->ipsclass->market_class->item_by_folder[$this->item['filename']]) == 1)
		{
			if(!isset($this->ipsclass->input['restore_groups']))
			{
				$this->ipsclass->html .= $this->ipsclass->adskin->start_form( array(
																			 	  	 1 => array( 'section'   , 'components'      ),
																					 2 => array( 'act'       , 'market'          ),
																					 3 => array( 'code'      , 'item'            ),
																					 4 => array( 'do'        , 'delete_item'     ),
																					 5 => array( 'item'      , $this->ipsclass->input['item']),
																			)		);
				
				$this->ipsclass->adskin->td_header[] = array( "" , "" );
				$this->ipsclass->adskin->td_header[] = array( "" , "" );
				
				$this->ipsclass->html .= $this->ipsclass->adskin->start_table("Deletion Info");
				
				$this->ipsclass->html .= $this->ipsclass->adskin->add_td_row(array(
																					"<b>Do you want to restore everyone to their original group?</b><br />Note: by not restoring the groups, you cannot restart the groups anymore.",
																					$this->ipsclass->adskin->form_yes_no('restore_groups', 1),
																			)	);
				
				$this->ipsclass->html .= $this->ipsclass->adskin->end_form("Remove Item");
				
				$this->ipsclass->html .= $this->ipsclass->adskin->end_table();
				
				$this->ipsclass->admin->output();
			}
			else
			{
				if($this->ipsclass->input['restore_groups'])
				{
					if(USE_MODULES == 1)
					{
						$this->ipsclass->DB->build_query(array('select'=>'id,old_mgroup','from'=>'members','where'=>"end_mgroup > 0"));
						$this->ipsclass->DB->exec_query();
						if($this->ipsclass->DB->get_num_rows())
						{
							require_once(ROOT_PATH."modules/ipb_member_sync.php");
							$modules = new ipb_member_sync();
							$modules->register_class(&$this);
							while($mem = $this->ipsclass->DB->fetch_row())
							{
								$modules->on_group_change($mem['id'],$mem['old_mgroup']);
							}
							$this->ipsclass->DB->build_and_exec_query(array('update'=>'members','set'=>'mgroup=old_mgroup','where'=>"end_mgroup > 0"));
						}
					}
					else
					{
						$this->ipsclass->DB->build_and_exec_query(array('update'=>'members','set'=>'mgroup=old_mgroup','where'=>"end_mgroup > 0"));
					}
				}
				$this->ipsclass->DB->sql_drop_field("members","old_mgroup");
				$this->ipsclass->DB->sql_drop_field("members","end_mgroup");
			}
		}
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		if(!is_array($this->ipsclass->cache['group_cache'][$this->item_config['group_id']]))
		{
			$this->ipsclass->Error(array('MSG'=>'invalid_group'));
		}
		
		if($this->item_config['group_id'] == $this->ipsclass->member['mgroup'])
		{
			$this->ipsclass->Error(array('MSG'=>'you_in_group'));
		}
		
		$this->ipsclass->DB->do_update('members',array(
														'mgroup' => $this->item_config['group_id'],
														'old_mgroup' => $this->ipsclass->member['mgroup'],
														'end_mgroup' => time() + $this->item_config['lasting'] * $this->item_config['time_unit'],
				),"id=".$this->ipsclass->member['id']);
		
		if(USE_MODULES == 1)
		{
			require_once(ROOT_PATH."modules/ipb_member_sync.php");
			$modules = new ipb_member_sync();
			$modules->register_class(&$this);
			$modules->on_group_change($this->ipsclass->member['id'],$this->item_config['group_id']);
		}
		
		$this->ipsclass->lang['change_to_x_group'] = str_replace('<#GROUP#>', $this->ipsclass->cache['group_cache'][$this->item_config['group_id']]['g_title'], $this->ipsclass->lang['change_to_x_group']);
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Changed group from {$this->ipsclass->cache['group_cache'][$this->ipsclass->member['mgroup']]['g_title']} to {$this->ipsclass->cache['group_cache'][$this->item_config['group_id']]['g_title']}");
		
		$this->ipsclass->market_class->redirect('change_to_x_group');
	}
	
	function parse_store_description()
	{
		return $this->description;
	}
	
	function parse_group_description()
	{
		return $this->description;
	}
	
	function parse_single_description()
	{
		return $this->description;
	}
}
?>